package _2022.hot100._13_罗马数字转整数;

/**
 * @author： YHSimon
 * @date： 2022-08-12 00:04
 */
public class Solution {
    public int romanToInt(String s) {
        int[] values = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4,1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int start=0;
        int res=0;
        for(int i=0;i<values.length;i++){
            int value=values[i];
            String symbol=symbols[i];
            while(start<s.length()&&start+symbol.length()<s.length()&&s.substring(start, start+symbol.length()).equals(symbol)){
                start=start+symbol.length();
                res+=value;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.romanToInt("LVIII"));
    }
}
